<?php
namespace app\admin\controller;
//导入Controller
use think\Controller;
//导入Db
use think\Db;
class Advert extends Allow
{
	//列表
    public function getindex()
    {
      $advert=Db::table("advert")->Paginate(3);
      // var_dump($advert);exit;
       return $this->fetch("Advert/index",["advert"=>$advert]);
    }
    //添加
    public function getadd(){
        return $this->fetch("Advert/add");
    }

    //执行添加
    public function postdoadd(){
        //文件上传
        $request=request();
        $data['title']=$request->param('title');
        $data['url']=$request->param('url');
        if($request->param('status')=='显示'){
          $data['status']=0;
        }else{
          $data['status']=1;
        }
        //获取上传的pic
        $file=$request->file('pic');
        $result=$this->validate(['file1'=>$file],['file1'=>'require|image'],['file1.require'=>'上传文件不能为空','file1'=>'上传文件必须是图片类型']);
        if(true !==$result){$this->error($result);}
        $info=$file->move(ROOT_PATH.'public'.DS.'uploads');
        $path=$info->getSavename();
        $data['pic']=$path;
        if($data['title']&&$data['url']){
        //执行添加
          if(Db::table("advert")->insert($data)){
              $this->success("广告添加成功","/adminadvert/index");
          }else{
              $this->error("广告添加失败,请重新添加");
           };
         }else{$this->error("标题和链接不能为空,请重新添加");}
    }

    public function getdelete(){
      $request=request();
      $id=$request->param('id');
      $data=Db::table("advert")->where("id","{$id}")->select();
      //获取图片所在路径
      $img="./uploads/".$data[0]['pic'];
      if(Db::table("advert")->where("id","{$id}")->delete()){
        //删除图片资源
        unlink($img);
        $this->success("广告删除成功","/adminadvert/index");
      }else{
        $this->error("广告删除失败");
      }
    }
    public function getedit(){
      $request=request();
      $id=$request->param('id');
      $data=Db::table("advert")->where("id","{$id}")->select();
      return $this->fetch("/advert/edit",["data"=>$data]);
    }
    public function postdoedit(){
      $request=request();
      //获取编辑之前图片地址,编辑成功则删除
      $src="./uploads/".$request->param('src');
      $id=$request->param("id");
      $data=$request->only(["title","status","url"]);
      $data['url']=$request->param("url");
      if($request->param('status')=='显示'){
          $data['status']=0;
        }else{
          $data['status']=1;
        }
      //获取上传的pic
        $file=$request->file('pic');
        $result=$this->validate(['file1'=>$file],['file1'=>'require|image'],['file1.require'=>'上传文件不能为空','file1'=>'上传文件必须是图片类型']);
        if(true !==$result){$this->error($result);}
        $info=$file->move(ROOT_PATH.'public'.DS.'uploads');
        $path=$info->getSavename();
        $data['pic']=$path;
        if($data['title']&&$data['url']){
      //执行编辑
            if(Db::table("advert")->where("id","{$id}")->update($data)){
              //删除修改之前的图片资源
              unlink($src);
              $this->success("广告修改成功","/adminadvert/index");
            }else{
              $this->error("广告修改失败");
            };
        }else{$this->error("标题和链接不能为空,请重新编辑");}
    }
    public function getdisplay(){
      $request=request();
      $id=$request->param('id');
      $status=$request->param('status');
      if(Db::execute("update advert set status='{$status}' where id='{$id}'")){
        $this->redirect('/adminadvert/index');
      }else{
        $this->redirect('/adminadvert/index');
      }
    }     
}
